package com.boot.until;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传统一处理
 */
@Slf4j
public class FileUpload {


    /**
     * 普通文件上传
     * @param file
     * @param request
     * @return
     */
    public static String upload(MultipartFile file, HttpServletRequest request){
        //获取文件类型
        String fileName=file.getOriginalFilename ();
        //获取后缀
        String suffixName=fileName.substring ( fileName.lastIndexOf ( "." ) );
        //重新命名
        fileName=System.currentTimeMillis ()/1000+suffixName;
        //创建项目路径(线上)
        //String path=request.getSession ().getServletContext ().getRealPath ( "upload" );
        //此处写死(线上)
        String path="/home/jun/ftpimg";
        log.info ( "【文件临时的路径】={}",path );
        log.info ( "【文件读取的路径】={}",path );
        //路径不存在就重新创建
        File pathFile=new File ( path );
        if (!pathFile.exists ()){
            pathFile.setWritable ( true );
            pathFile.mkdirs ();
        }
        File targetFile=new File (path,fileName);
        //log.info ( "file",targetFile);
        try {
            file.transferTo ( targetFile );
           log.info ("file={}",targetFile.toString ());
          /*  //文件上传到Ftp服务器
            List<File> fileList=new ArrayList<> (  );
            fileList.add ( targetFile );
            if(FtpUntil.uploadFile ( fileList )){*/
                //删除原文件
                //targetFile.delete ();
                return "https://www.dragonjun.com/ftpimg/"+fileName;
           // }
        } catch (IOException e) {
            log.info ( "上传文件异常={}",e);
        }
        return null;
    }



    /**
     * base文件上传
     * @param baseImg 加密码
     * @param request
     * @return
     */
    public static String uploadBase(String baseImg, HttpServletRequest request){
        //创建项目路径
        String path=request.getSession ().getServletContext ().getRealPath ( "upload" );
        log.info ( "【文件临时的路径】={}",path );
        String fileName="/"+System.currentTimeMillis ()/1000+".jpg";
        //路径不存在就重新创建
        File pathFile=new File ( path );
        if (!pathFile.exists ()){
            pathFile.setWritable ( true );
            pathFile.mkdirs ();
        }

        //开始装换图片
        Boolean isSuccess= false;
        File targetFile=null;
        try {
            isSuccess = Base64Until.generateImage ( baseImg,path+fileName);
            if (isSuccess){
                targetFile=new File (path,fileName);
            }
            //文件上传到Ftp服务器
            List<File> fileList=new ArrayList<> (  );
            fileList.add ( targetFile );
            if(FtpUntil.uploadFile ( fileList )){
                //删除原文件
                targetFile.delete ();
                return "http://www.dragonjun.com/ftpimg"+fileName;
            }
        } catch (Exception e) {
            log.info ( "上传文件异常={}",e);
        }
        return "54646";
    }





}
